﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Hi_Baidu;

namespace Hi_Baidu.Net
{
    class SystemMessageListener : MessageListener
    {
        public SystemMessageListener(int ReadLength):base(ReadLength)
        {
            
        }

        public override void Init()
        {
            //创建监听线程
            this.CurrentThread = new Thread(new ThreadStart(this.Listen));
            CurrentThread.Name = "Listener Message of TCP Port";
            CurrentThread.Start();
        }

        protected override void Listen()
        {
           NetworkStream GetMessageStream = new NetworkStream(this.UserSocket);
           while(IS_LOOP_GET_MESSAGE)
           {
               byte[] Content = new byte[this.ReadLength];
               //读取数据
               int Length = GetMessageStream.Read(Content, 0, Content.Length);
               //打入信息池
               this.NetworkMessagePool.Write(Content, Length);
           }
        }

        public override void Dispose()
        {
            IS_LOOP_GET_MESSAGE = false;

            try
            {
                if (this.UserSocket != null)
                {
                    this.UserSocket.Close();
                }
            }
            catch
            {}

            if (this.CurrentThread != null)
            {
                this.CurrentThread.Abort();
            }
        }
    }
}
